package com.exchange.used.service.impl;

import com.exchange.used.config.converters.LayuiResult;
import com.exchange.used.entity.Order;
import com.exchange.used.entity.Record;
import com.exchange.used.entity.User;
import com.exchange.used.repository.OrderRepository;
import com.exchange.used.repository.RecordRepository;
import com.exchange.used.repository.UserRepository;
import com.exchange.used.service.RecordServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("RecordService")
public class RecordService implements RecordServiceI {

    @Autowired
    private RecordRepository recordRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private OrderRepository orderRepository;

    @Override
    public LayuiResult getRemark(String goodsid) {
        LayuiResult result = new LayuiResult();
        try {
            List<Record> records = recordRepository.getRecordsByFkgoodsidAndRemarktimeIsNotNull(goodsid);
            List<Map<String, Object>> resultList = new ArrayList<>();
            for(Record record : records){
                Map<String, Object> resultMap = new HashMap<String, Object>(BeanMap.create(record));
                Order order = orderRepository.getOrderByOrderid(record.getFkorderid());
                User user = userRepository.getUserByUserid(order.getFkbuyerid());
                resultMap.putAll(BeanMap.create(order));
                resultMap.putAll(BeanMap.create(user));
                resultList.add(resultMap);
            }
            result.getData().put("recordList", resultList);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(-1);
            result.setMsg(e.getMessage());
        }

        return result;
    }
}
